/*
 * @Author: Zhengxuan Ye
 * @Date: 2025-05-06 21:28:38
 * @Last Modified by: Zhengxuan Ye
 * @Last Modified time: 2025-05-06 21:28:38
 */

<template>
  <div class="admin-login-container">
    <div class="login-card">
      <h1 class="login-title">管理员登录</h1>
      <p class="login-subtitle">请输入管理员账号和密码</p>

      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="username"
            placeholder="请输入管理员用户名"
            required
          />
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="password"
            placeholder="请输入管理员密码"
            required
          />
        </div>

        <div v-if="error" class="error-message">
          {{ error }}
        </div>

        <button type="submit" class="btn-login" :disabled="loading">
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>

      <div class="login-footer">
        <p>本页面仅供管理员登录使用</p>
        <router-link to="/" class="back-link">返回首页</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { useUserStore } from '../../stores/user'

export default {
  name: 'AdminLoginView',
  setup() {
    const router = useRouter()
    const userStore = useUserStore()
    
    const username = ref('')
    const password = ref('')
    const error = ref('')
    const loading = ref(false)

    const handleLogin = async () => {
      try {
        loading.value = true
        error.value = ''

        // 调用管理员登录API
        const response = await axios.post('http://localhost:3000/api/admin/login', {
          username: username.value,
          password: password.value
        })

        // 登录成功，保存token和管理员信息
        const { token, admin } = response.data
        
        // 使用userStore保存登录状态
        userStore.setToken(token)
        userStore.setUser({
          ...admin,
          isAdmin: true
        })
        
        // 跳转到管理后台
        router.push('/admin')
      } catch (err) {
        console.error('管理员登录失败:', err)
        error.value = err.response?.data?.message || '登录失败，请检查用户名和密码'
      } finally {
        loading.value = false
      }
    }

    return {
      username,
      password,
      error,
      loading,
      handleLogin
    }
  }
}
</script>

<style scoped>
.admin-login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 70vh;
  padding: 2rem 0;
}

.login-card {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(139, 69, 19, 0.15);
  padding: 2.5rem;
  width: 100%;
  max-width: 450px;
  border-top: 4px solid var(--primary-color);
}

.login-title {
  font-family: 'ZCOOL QingKe HuangYou', sans-serif;
  color: var(--primary-color);
  font-size: 2rem;
  margin-bottom: 0.5rem;
  text-align: center;
}

.login-subtitle {
  color: #666;
  text-align: center;
  margin-bottom: 2rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-group label {
  font-weight: bold;
  color: var(--dark-color);
}

.form-group input {
  padding: 0.8rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  transition: all 0.3s ease;
}

.form-group input:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(184, 134, 11, 0.2);
  outline: none;
}

.error-message {
  color: #f56c6c;
  background-color: rgba(245, 108, 108, 0.1);
  padding: 0.8rem;
  border-radius: 4px;
  text-align: center;
}

.btn-login {
  background-color: var(--primary-color);
  color: white;
  border: none;
  padding: 0.8rem;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: bold;
}

.btn-login:hover {
  background-color: var(--accent-color);
}

.btn-login:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.login-footer {
  margin-top: 2rem;
  text-align: center;
  color: #666;
}

.back-link {
  display: inline-block;
  margin-top: 0.5rem;
  color: var(--primary-color);
  text-decoration: none;
}

.back-link:hover {
  text-decoration: underline;
}

@media (max-width: 768px) {
  .login-card {
    padding: 1.5rem;
    margin: 0 1rem;
  }
}
</style>
